/**
  ******************************************************************************
  * @file    ring_buffer.h
  * @author  ashuai0110
  * @version V2.3
  * @date    2024-07-01
  * @brief   环形缓冲区
  *          (适合定长或易分离出的数据,提供覆写函数和非覆写函数)
  *
  ******************************************************************************
  * @attention
  *
  * 版权声明:内容为编者(ashuai0110)原创,使用请注明出处,当然,你也可以不这样做^_^
  * 出处链接:https://gitee.com/ashuai0110/mcu_reuse_development_module.git
  *
  ******************************************************************************
  */

#ifndef __RING_BUFFER_H
#define __RING_BUFFER_H

#ifdef __cplusplus
extern "C" {
#endif

/* 包含头文件-----------------------------------------------------------------*/
#include "common_include.h"

/**
 * @addtogroup modules
 * @{
 */

/**
 * @addtogroup ring_buffer
 * @{
 */

/* 宏定义---------------------------------------------------------------------*/

/* 类型定义-------------------------------------------------------------------*/
/**
 * @defgroup ring_buffer_global_types ring buffer global types
 * @{
 */

/** 
  * @brief  环形缓冲区 structrue definition
  */
typedef struct ring_buffer {
    char *pHead;     /*!< 头节点(开始读节点) */
    char *pTail;     /*!< 尾节点(开始写节点) */
    char *pBuf;      /*!< 数据缓冲区 */
    uint32_t bufLen; /*!< 数据缓冲区大小 */
} ring_buffer_t;

/**
 * @}
 */

/* 全局变量-------------------------------------------------------------------*/

/* 函数原型-------------------------------------------------------------------*/
/**
 * @addtogroup ring_buffer_global_functions
 * @{
 */

void ring_buffer_init(ring_buffer_t* _rb, void *pBuf, uint32_t bufLen);
void ring_buffer_de_init(ring_buffer_t *_rb);

uint32_t ring_buffer_can_read(ring_buffer_t *_rb);
uint32_t ring_buffer_can_write(ring_buffer_t *_rb);

uint32_t ring_buffer_read(ring_buffer_t *_rb, void *pBuf, uint32_t len);
uint32_t ring_buffer_write(ring_buffer_t *_rb, void *pData, uint32_t len);
uint32_t ring_buffer_overwrite(ring_buffer_t *_rb, void *pData, uint32_t len);

/* set/get interface */
void ring_buffer_set_read_offset(ring_buffer_t *_rb, uint32_t offset);
uint32_t ring_buffer_get_read_offset(ring_buffer_t *_rb);
void ring_buffer_set_write_offset(ring_buffer_t *_rb, uint32_t offset);
uint32_t ring_buffer_get_write_offset(ring_buffer_t *_rb);

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif /* ring_buffer.h */
